package com.snopy.socket;

import lombok.extern.slf4j.Slf4j;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;

/**
 * @author snopy
 * @version 1.0.0
 * @classname KockKockClient
 * @description
 * @email 77912204@qq.com
 * @date 2022/08/01 15:42
 */
@Slf4j
public class KnockKnockClient {
    public static void main(String[] args) {
        if (args.length!=2){
            log.error("Usage: java EcohClient <hostName> <portNumber>");
            System.exit(1);
        }
        String hostName = args[0];
        int portNumber = Integer.parseInt(args[1]);
        try(Socket socket = new Socket(hostName,portNumber)) {
            PrintWriter out = new PrintWriter(socket.getOutputStream(),true);
            BufferedReader in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            BufferedReader stdIn = new BufferedReader(new InputStreamReader(System.in));
            String fromServer;
            String fromUser;
            while ((fromServer = in.readLine())!=null){
                log.info("Server:{}",fromServer);
                if (fromServer.equals("Bye.")){
                    break;
                }
                fromUser = stdIn.readLine();
                if (fromUser!=null){
                    log.info("Client:{}",fromUser);
                    out.println(fromUser);
                }
            }


        } catch (IOException e) {
            log.error(e.getMessage());
            System.exit(1);
        }


    }
}
